//
//  ZYStudentSearchController.m
//  优学荟管理系统
//
//  Created by Ethan on 2017/10/7.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYStudentSearchController.h"
#import "ZYStudentDetailTowModel.h"
#import "ZYStudentDetailTowCell.h"
#define kCell @"cell"
@interface ZYStudentSearchController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UISearchBar *searchBar;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isTrue;

@end

@implementation ZYStudentSearchController

- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource  = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"搜索结果";
    
    [self setUpSuperView];
    [self setUpRefresh];
    
}
#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}







- (void)setNetWork{
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=bindStudentsDetial&search=%@&pageNumber=%ld",self.textStr,(long)self.page];
    
     NSString *changeUilStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
            
            NSArray *arr = [dict valueForKey:@"dataList"];
            
            
            for (NSDictionary *dic in arr) {
                
                ZYStudentDetailTowModel *model = [ZYStudentDetailTowModel new];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.dataSource addObject:model];
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
}













#pragma mark -----子视图
- (void)setUpSuperView{
    
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight -64)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 200;
    
    
    [self.tableView registerClass:[ZYStudentDetailTowCell  class] forCellReuseIdentifier:kCell];
    
    
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYStudentDetailTowCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    
    
    if (self.dataSource.count > indexPath.row) {
        
        
        ZYStudentDetailTowModel *model = self.dataSource[indexPath.row];
        
        
        
        [cell assignByZYStudentDetailTowModel:model];
        
        
    }
    
    
    [cell.phoneButton addTarget:self action:@selector(phoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}



- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
  
    
    [SVProgressHUD dismiss];
    
}

- (void)phoneButtonAction:(UIButton *)sender{
    
    ZYStudentDetailTowCell *cell = (ZYStudentDetailTowCell *)sender.superview.superview;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYStudentDetailTowModel *model = self.dataSource[indexPath.row];
    
    
    
    
    
    [self phoneChat:[NSString stringWithFormat:@"%@",[model.owner valueForKey:@"phone"]]];
    
    
    
    
}

- (void)phoneChat:(NSString *)phoneNumber{
    
    NSString *strs = phoneNumber;
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",strs];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}



@end
